package com.stu.boke.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.BasePageHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stu.boke.entity.Article;
import com.stu.boke.entity.ArticleCategoryRef;
import com.stu.boke.entity.ArticleTagRef;
import com.stu.boke.entity.Category;
import com.stu.boke.entity.Tag;
import com.stu.boke.enums.ArticleCommentStatus;
import com.stu.boke.mapper.ArticleCategoryRefMapper;
import com.stu.boke.mapper.ArticleMapper;
import com.stu.boke.mapper.ArticleTagRefMapper;
import com.stu.boke.service.ArticleService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService{
	
	@Autowired(required = false)
	private ArticleMapper articleMapper;
	
	@Autowired(required = false)
	private ArticleCategoryRefMapper articleCategoryRefMapper;
	
	@Autowired(required = false)
	private ArticleTagRefMapper articleTagRefMapper;
	
	@Override
	public List<Article> listRecentArticle(int limit) {
		return articleMapper.listArticleByLimit(limit);
	}

	@Override
	public Integer countArticleComment() {
		return articleMapper.countArticleComment();
	}

	@Override
	public Integer countArticleView() {
		return articleMapper.countArticleView();
	}

	@Override
	public Article getLastUpdateArticle() {
		return articleMapper.getLastUpdateArticle();
	}

	@Override
	public Integer countArticle(Integer status) {
		Integer count = 0;
        try {
            count = articleMapper.countArticle(status);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据状态统计文章数, status:{}, cause:{}", status, e);
        }
        return count;
	}

	@Override
	public PageInfo<Article> pageArticle(Integer pageIndex, Integer pageSize, HashMap<String, Object> criteria) {
		BasePageHelper.startPage(pageIndex, pageSize);
		List<Article> articleList = articleMapper.findAll(criteria);
		for (int i = 0; i < articleList.size(); i++) {
			List<Category> categoryList = articleCategoryRefMapper.listCategoryByArticleId(articleList.get(i).getArticleId());
			if(categoryList == null || categoryList.size() == 0) {
				categoryList = new  ArrayList<>();
				categoryList.add(Category.Default());
			}
			articleList.get(i).setCategoryList(categoryList);
		}
		return new PageInfo<>(articleList);
	}

	@Override
	public Article getArticleByStatusAndId(Integer status, Integer id) {
		Article article = articleMapper.getArticleByStatusAndId(status,id);
		if(null != article) {
			List<Category> categoryList = articleCategoryRefMapper.listCategoryByArticleId(id);
			List<Tag> tagList = articleTagRefMapper.listTagByArticleId(id);
			article.setTagList(tagList);
			article.setCategoryList(categoryList);
		}
		return article;
	}

	@Override
	public void deleteArticle(Integer id) {
		articleMapper.deleteById(id);
	}

	@Override
	public void insertArticle(Article article) {
		//添加文章
		article.setArticleCreateTime(new Date());
        article.setArticleUpdateTime(new Date());
        article.setArticleIsComment(ArticleCommentStatus.ALLOW.getValue());
        article.setArticleViewCount(0);
        article.setArticleLikeCount(0);
        article.setArticleCommentCount(0);
        article.setArticleOrder(1);
        articleMapper.insert(article);
        //添加分类和文章关联
        for (int i = 0; i < article.getCategoryList().size(); i++) {
            ArticleCategoryRef articleCategoryRef = new ArticleCategoryRef(article.getArticleId(), article.getCategoryList().get(i).getCategoryId());
            articleCategoryRefMapper.insert(articleCategoryRef);
        }
        //添加标签和文章关联
        for (int i = 0; i < article.getTagList().size(); i++) {
            ArticleTagRef articleTagRef = new ArticleTagRef(article.getArticleId(), article.getTagList().get(i).getTagId());
            articleTagRefMapper.insert(articleTagRef);
        }
	}

	@Override
	public void updateArticle(Article article) {
		articleMapper.update(article);
	}

	@Override
	public List<Article> listArticleByCommentCount(int limit) {
		return articleMapper.listArticleByCommentCount(limit);
	}

	@Override
	public List<Integer> listCategoryIdByArticleId(Integer articleId) {
		return articleCategoryRefMapper.selectCategoryIdByArticleId(articleId);
	}

	@Override
	public List<Article> listArticleByCategoryIds(List<Integer> categoryIds, int limit) {
		  if (categoryIds == null || categoryIds.size() == 0) {
	            return null;
	        }
	        return articleMapper.findArticleByCategoryIds(categoryIds, limit);
	}

	@Override
	public List<Article> listArticleByViewCount(int limit) {
		return articleMapper.listArticleByViewCount(limit);
	}

	@Override
	public Article getAfterArticle(Integer articleId) {
		return articleMapper.getAfterArticle(articleId);
	}

	@Override
	public Article getPreArticle(Integer articleId) {
		return articleMapper.getPreArticle(articleId);
	}

	@Override
	public List<Article> listRandomArticle(int limit) {
		return articleMapper.listRandomArticle(limit);
	}

	@Override
	public List<Article> listAllNotWithContent() {
		return articleMapper.listAllNotWithContent();
	}

	@Override
	public void updateCommentCount(Integer articleId) {
		articleMapper.updateCommentCount(articleId);
	}


}
